<?php

namespace app\modules\web\controllers;


use app\models\User;
use app\modules\web\controllers\common\BaseController;
use yii\helpers\Url;


class UserController extends BaseController
{
    public function __construct($id, $module, array $config = [])
    {
        parent::__construct($id, $module, $config);
        $this->layout="main";
    }

    //登录页面
    public function actionLogin()
    {
        //判断请求方法 get展示页面 post 处理逻辑代码
        if(\Yii::$app->request->isGet){
            $this->layout=false;
            return $this->render("login");
        }
        //var_dump(\Yii::$app->request->post());
        $login_name=trim($this->post('login_name'));
        $login_pwd=trim($this->post('login_pwd'));

        if(!$login_name || !$login_pwd){
            return $this->renderJs('请输入正确的用户名和密码1',Url::to(['']));
        }

        //从用户表获取 login_name=$login_name 信息是否存在
        $user_info=User::find()->where(['login_name'=>$login_name])->one();
        if(!$user_info){
            return $this->renderJs('请输入正确的用户名和密码2',Url::to(['']));
        }

        //验证密码 密码算法
        //密码算法=md5( login_pwd + md5(login_salt))
        //$auth_pwd=md5($login_pwd.md5($user_info['login_salt']));

        if(!$user_info->verifyPassword($login_pwd)){
            return $this->renderJs('请输入正确的用户名和密码3',Url::to(['']));
        }

        //保存用户登录状态
        //cookie保存用户登录状态
        //cookie的值包括 加密字符串+‘#’+uid  加密字符串=md5(login_name+login_pwd+login_salt);
        //设置加密字符串
        $this->setLoginStatus($user_info);
        return $this->redirect(['/web/dashboard/index']);

    }

    //当前登录人的编辑页面
    public function actionEdit()
    {
        if(\Yii::$app->request->isGet){
            return $this->render("edit",['user_info'=>$this->current_user]);
        }

        //接收ajax post方法提交过来的数据
        $nickname=trim($this->post("nickname",""));
        $email=trim($this->post("email",""));

        //mb_strlen()函数判断nickname 和email的长度 从而能判断两个值是否为空
        if(mb_strlen($nickname,"utf-8")<1){
            return $this->renderJson([],'请输入正确的用户名~',-1);
        }

        if(mb_strlen($email,"utf-8")<1){
            return $this->renderJson([],'请输入正确的邮箱~',-1);
        }

        //取得当前登录对象信息
        $user_info=$this->current_user;
        //更新该对象的信息
        $user_info->nickname=$nickname;
        $user_info->email=$email;
        $user_info->updated_time=date("Y-m-d H:i:s");
        //执行更新
        $user_info->update(0);
        return $this->renderJson([],"编辑成功~");

    }

    //当前登录人重置密码页面
    public function actionRestPwd()
    {
        if(\Yii::$app->request->isGet){
            return $this->render("rest_pwd",['user_info'=>$this->current_user]);
        }

        $old_password=trim($this->post("old_password",""));
        $new_password=trim($this->post("new_password",""));

        if(mb_strlen($old_password,"utf-8")<1){
            return $this->renderJson([],'请输入原密码',-1);
        }
        if(mb_strlen($new_password,"utf-8")<6){
            return $this->renderJson([],'新密码不能少于6位',-1);
        }

        if($old_password===$new_password){
            return $this->renderJson([],'新密码和老密码相同',-1);
        }

        //判断原密码是否正确
        $user_info=$this->current_user;
        //$auth_pwd=md5($old_password.md5($user_info['login_salt']));

        if(!$user_info->verifyPassword($old_password)){
            return $this->renderJson([],'请确认原密码正确',-1);
        }

        //$user_info->login_pwd=md5($new_password.md5($user_info['login_salt']));
        $user_info->setPassword($new_password);
        $user_info->updated_time=date("Y-m-d H:i:s");
        $user_info->update(0);

        //设置修改密码后的登录态
        $this->setLoginStatus($user_info);
        return $this->renderJson([],'密码修改成功');

    }

    /**
     * 登录退出方法
     */
    public function actionLoginOut()
    {
        $this->removeStatus();
        return $this->redirect(Url::to(['/web/user/login']));
    }
}
